<?php
namespace Controllers;
use Resources,
    Libraries;
class KasMasuk extends Libraries\PController {
    private $id, $doc_ref, $no_bukti, $amount, $entry_time, $trans_date,
            $trans_via, $note, $id_bank, $id_kavling;
    public function __construct() {
        parent::__construct();
        $this->db = new Libraries\Mysql;
    }
    public function index() {
        if (Libraries\u::isAjaxRequest()) {
            $kas_masuk = $this->db->results(Libraries\CDb::get_kas_masuk());
            $this->outputJSON(array('success' => 'true', 'results' => $kas_masuk));
        } else {
            $this->redirect('home');
        }
    }
    public function view() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $kas_masuk = $this->db->results(Libraries\CDb::view_kas_masuk($this->id));
            $this->outputJSON(array('success' => 'true', 'results' => $kas_masuk));
        } else {
            $this->redirect('home');
        }
    }
    public function create() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $status = false;
            $msg = '';
            $this->db->begin();
            try {
                $ref = new Libraries\ReferenceCom($this->db);
                $this->doc_ref = $ref->get_next_reference(KAS_MASUK);
                $this->db->results(Libraries\CDb::insert_kas_masuk($this->doc_ref,
                                $this->no_bukti, $this->amount, null,
                                $this->trans_date, $this->trans_via,
                                $this->note, $this->id_bank, $this->id_kavling));
                $id = $this->db->insertId();
                $ref->save(KAS_MASUK, $id, $this->doc_ref);
                $this->db->results(
                        Libraries\CDb::save_bank_trans(KAS_MASUK, $id,
                                $this->doc_ref, $this->trans_date,
                                $this->amount, $this->id_bank, '-',
                                $this->id_user, $this->id_kavling));
                $this->db->commit();
            } catch (Exception $e) {
                $this->db->rollback();
                $status = false;
                $msg = $e->getMessage();
            }
            $id = $this->db->insertId();
            $msg = "Data berhasil di simpan dengan referensi $this->doc_ref";
            $this->outputJSON(array(
                'success' => $status,
                'msg' => $msg
            ));
        } else {
            $this->redirect('home');
        }
    }
    public function delete() {
        if (Libraries\u::isAjaxRequest()) {
            $id = $this->get_post('id');
            $memo_ = $this->get_post('memo_');
            $status = false;
            $msg = 'Kas masuk berhasil divoid.';
            $user_id = Libraries\u::get_user_id($this->session);
            $this->db->begin();
            try {
                $ref = new Libraries\ReferenceCom($this->db);
                $this->doc_ref = $ref->get_next_reference(VOID);
                $kas_masuk = $this->db->row(Libraries\CDb::get_kas_masuk_by_id($id));
                $this->db->results(Libraries\CDb::insert_voided(KAS_MASUK,
                                $kas_masuk->kas_masuk_id, $kas_masuk->trans_date,
                                $memo_));
                $id = $this->db->insertId();
                $ref->save(VOID, $id, $this->doc_ref);
                $this->db->results(
                        Libraries\CDb::save_bank_trans(VOID, $id,
                                $this->doc_ref, $kas_masuk->trans_date,
                                -$kas_masuk->amount, $kas_masuk->id_bank, '-',
                                $user_id, $kas_masuk->id_kavling));
                $this->db->commit();
            } catch (Exception $e) {
                $this->db->rollback();
                $status = false;
                $msg = $e->getMessage();
            }
            $id = $this->db->insertId();
            $msg = "Data berhasil di simpan dengan referensi $this->doc_ref";
            $this->outputJSON(array(
                'success' => $status,
                'msg' => $msg
            ));
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    private function get_submit() {
        foreach ($_POST as $key => $val) {
            $_POST[$key] = $this->request->post($key,
                    FILTER_SANITIZE_MAGIC_QUOTES);
            $_POST[$key] = $this->db->escape($_POST[$key]);
            $this->$key = $val;
        }
        $this->amount = Libraries\u::get_number($this->amount);
        $this->id_user = Libraries\u::get_user_id($this->session);
    }
}
